// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: VS Game Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias 37 : TYPE_VSGAME


// Static Values

// Tables

event ObjectMain
	switch object.state
	case 0
		if vs.playerID == 0
			if oscillation == 500
				CallNativeFunction2(TransmitGlobal, "vs.randomValue", vs.randomValue)
			end if
		end if
		if object[0].state != PlayerObject_Blank
			object.value1 = object[0].xpos
			object.value2 = object[0].ypos
		end if
		if object[1].state != PlayerObject_Blank
			object.value3 = object[1].xpos
			object.value4 = object[1].ypos
		end if
		break
	case 1
		if object.value0 < 384
			object.value0 += 16
			if object[0].state == PlayerObject_Blank
				object.state = 2
			end if
			if object[1].state == PlayerObject_Blank
				object.state = 2
			end if
		else
			object.state = 2
			if object[1].type != TypeName[Blank Object]
				object[0].state = PlayerObject_HandleAir
				object[0].xpos = object[30].value3
				object[0].ypos = object[30].value4
				object[1].state = PlayerObject_HandleAir
				object[1].xpos = object[30].value1
				object[1].ypos = object[30].value2
				screen.cameraX = object[0].ixpos
				screen.cameraY = object[0].iypos
				temp0 = object[0].collisionPlane
				object[0].collisionPlane = object[1].collisionPlane
				object[1].collisionPlane = temp0
				temp0 = object[0].value37
				object[0].value37 = object[1].value37
				object[1].value37 = temp0
				arrayPos0 = 0
				arrayPos0 += playerCount
				temp0 = object[arrayPos0].type
				if PlayerObject_SuperState == 1
					temp0 = invincibilityType
					object[0].value7 = 0x4EC
				end if
				arrayPos1 = 1
				arrayPos1 += playerCount
				temp1 = object[arrayPos1].type
				if temp0 == temp1
					if temp0 != invincibilityType
						currentPlayer = 0
						CallFunction(PlayerObject_RestorePowerup)
						currentPlayer = 1
						arrayPos0 = arrayPos1
						CallFunction(PlayerObject_RestorePowerup)
					else
						temp0 = object[0].value7
						object[0].value7 = object[1].value7
						object[1].value7 = temp0
					end if
				else
					if temp0 == invincibilityType
						currentPlayer = 0
						CallFunction(PlayerObject_RestorePowerup)
						ResetObjectEntity(arrayPos1, invincibilityType, 0, object[1].xpos, object[1].ypos)
						object[arrayPos1].priority = PRIORITY_ACTIVE
						temp0 = object[0].value7
						object[0].value7 = object[1].value7
						object[1].value7 = temp0
					else
						if temp1 == invincibilityType
							ResetObjectEntity(arrayPos0, invincibilityType, 0, object[0].xpos, object[0].ypos)
							object[arrayPos0].priority = PRIORITY_ACTIVE
							currentPlayer = 1
							arrayPos0 = arrayPos1
							CallFunction(PlayerObject_RestorePowerup)
							temp0 = object[0].value7
							object[0].value7 = object[1].value7
							object[1].value7 = temp0
						else
							currentPlayer = 0
							CallFunction(PlayerObject_RestorePowerup)
							currentPlayer = 1
							arrayPos0 = arrayPos1
							CallFunction(PlayerObject_RestorePowerup)
						end if
					end if
				end if
				temp0 = object[0].value6
				object[0].value6 = object[1].value6
				object[1].value6 = temp0
				currentPlayer = 0
				CallFunction(PlayerObject_SwitchPhysicsState)
				currentPlayer = 1
				CallFunction(PlayerObject_SwitchPhysicsState)
				foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
					switch object[currentPlayer].propertyValue
					case 1
					case 2
						if object[currentPlayer].value37 > 1
							object[currentPlayer].value37 = 1
							arrayPos0 = currentPlayer
							arrayPos0 += playerCount
							if object[arrayPos0].type != invincibilityType
								CallFunction(PlayerObject_RestorePowerup)
							end if
						end if
					end switch
				next
			end if
		end if
		SetScreenFade(208, 255, 224, object.value0)
		break
	case 2
		if object.value0 > 0
			object.value0 -= 16
		else
			object.type = TypeName[Blank Object]
		end if
		SetScreenFade(208, 255, 224, object.value0)
		break
	case 3
		if object.value6 > 0
			object.value0++
			if object.value0 == 60
				object.value0 = 0
				object.value6--
				if object.value6 == 0
					currentPlayer = object.value5
					if object[currentPlayer].state != PlayerObject_Death
						if object[currentPlayer].type != TypeName[Death Event]
							if currentPlayer == 0
								if vs.playerID == 0
									temp2 = 0
								else
									temp2 = 1
								end if
							else
								if vs.playerID == 0
									temp2 = 1
								else
									temp2 = 0
								end if
							end if
							if temp2 == 0
								vs.time1P = stage.seconds
								vs.time1P *= 100
								temp2 = stage.minutes
								temp2 *= 0x1770
								vs.time1P += temp2
								vs.time1P += stage.milliSeconds
							else
								vs.time2P = stage.seconds
								vs.time2P *= 100
								temp2 = stage.minutes
								temp2 *= 0x1770
								vs.time2P += temp2
								vs.time2P += stage.milliSeconds
							end if
							stage.timeOver = 1
							CallFunction(PlayerObject_Kill)
						end if
					end if
				end if
			end if
		end if
		break
	case 4
		if object.value0 < 384
			object.value0 += 16
		else
			if vs.playerID == 0
				vs.rings1P = object[0].value0
				vs.rings2P = object[1].value0
			else
				vs.rings1P = object[1].value0
				vs.rings2P = object[0].value0
			end if
			stage.activeList = PRESENTATION_STAGE
			stage.listPos = 3
			LoadStage()
		end if
		if object.value0 > 0
			SetScreenFade(0, 0, 0, object.value0)
		end if
		break
	case 5
		if object.value0 == 320
			object[0].value0 = vs.rings1P
			object[0].speed = vs.speed
			object[0].xvel = vs.speed
			object[0].yvel = 0
			object[0].gravity = GRAVITY_GROUND
			object[0].value37 = vs.items1P
			temp0 = currentPlayer
			temp1 = arrayPos0
			currentPlayer = 0
			arrayPos0 = playerCount
			arrayPos0 += currentPlayer
			CallFunction(PlayerObject_RestorePowerup)
			currentPlayer = temp0
			arrayPos0 = temp1
			temp0 = vs.time1P
			temp0 /= 0x1770
			stage.minutes = temp0
			temp0 = vs.time1P
			temp0 /= 100
			temp0 %= 60
			stage.seconds = temp0
			temp0 = vs.time1P
			temp0 %= 100
			stage.milliSeconds = temp0
			TitleCard_value23 = 1
		end if
		if object.value0 < 256
			stage.state = STAGE_RUNNING
			stage.timeEnabled = 1
			object[0].visible = 1
		else
			if stage.listPos == 2
				object[0].ypos = object.ypos
			end if
		end if
		if object.value0 > 0
			object.value0 -= 16
		else
			object.type = TypeName[Blank Object]
		end if
		SetScreenFade(0, 0, 0, object.value0)
		break
	case 6
		if object.value0 > -1
			if object.value0 < 320
				object.value0 += 16
			else
				vs.rings1P = object[0].value0
				vs.time1P = stage.seconds
				vs.time1P *= 100
				temp2 = stage.minutes
				temp2 *= 0x1770
				vs.time1P += temp2
				vs.time1P += stage.milliSeconds
				vs.items1P = object[0].value37
				vs.speed = object[0].speed
				stage.listPos++
				LoadStage()
			end if
			SetScreenFade(0, 0, 0, object.value0)
		else
			if object[0].xpos > object.xpos
				object.value0 = 0
			end if
		end if
		break
	end switch
end event


event ObjectDraw
	switch object.state
	case 0
		temp0 = vs.playerID
		temp0 ^= 1
		DrawSpriteXY(temp0, object[1].xpos, object[1].ypos)
		break
	case 3
		temp0 = vs.playerID
		temp0 ^= 1
		DrawSpriteXY(temp0, object[1].xpos, object[1].ypos)
		temp0 = object.value6
		temp0 /= 10
		temp0 += 2
		temp1 = screen.xcenter
		temp1 -= 8
		DrawSpriteScreenXY(temp0, temp1, 13)
		temp0 = object.value6
		temp0 %= 10
		temp0 += 2
		temp1 += 8
		DrawSpriteScreenXY(temp0, temp1, 13)
		break
	case 5
		if object.value0 > 256
			DrawRect(0, 0, screen.xsize, screen.ysize, 0, 0, 0, 255)
		end if
		break
	end switch
end event


event ObjectStartup
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-8, -32, 16, 7, 103, 48)
	SpriteFrame(-8, -32, 16, 7, 103, 56)
	SpriteFrame(0, 0, 8, 11, 1, 50)
	SpriteFrame(0, 0, 8, 11, 10, 50)
	SpriteFrame(0, 0, 8, 11, 19, 50)
	SpriteFrame(0, 0, 8, 11, 28, 50)
	SpriteFrame(0, 0, 8, 11, 1, 62)
	SpriteFrame(0, 0, 8, 11, 10, 62)
	SpriteFrame(0, 0, 8, 11, 19, 62)
	SpriteFrame(0, 0, 8, 11, 28, 62)
	SpriteFrame(0, 0, 8, 11, 1, 74)
	SpriteFrame(0, 0, 8, 11, 10, 74)
	temp0 = 0
	foreach (TypeName[VS Game], arrayPos0, ALL_ENTITIES)
		switch object[arrayPos0].propertyValue
		case 1
			object[arrayPos0].state = 5
			object[arrayPos0].value0 = 320
			object[arrayPos0].priority = PRIORITY_ACTIVE_PAUSED
			object[arrayPos0].drawOrder = 6
			object[0].visible = 0
			stage.state = STAGE_PAUSED
			temp0++
			break
		case 2
			object[arrayPos0].state = 6
			object[arrayPos0].value0 = -1
			object[arrayPos0].priority = PRIORITY_XBOUNDS
			temp0++
			break
		default
			object[arrayPos0].type = TypeName[Blank Object]
		end switch
	next
	if temp0 == 0
		vs.bossAttack = 0
	end if
	if options.vsMode == 1
		object[30].type = TypeName[VS Game]
		object[30].priority = PRIORITY_ACTIVE
		object[30].drawOrder = 5
		object[30].value6 = 60
	end if
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
